package com.luxiu.extension.extensionCardView

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import androidx.cardview.widget.CardView
import androidx.dynamicanimation.animation.DynamicAnimation
import androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationUpdateListener
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
import com.luxiu.extension.R

public open class AnimationCardView(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
) : CardView(context, attrs, defStyleAttr) {
    private var mSpringAnimation: SpringAnimation? = null
    var isNoAnimation: Boolean = false
    private var mUpdateListener: OnAnimationUpdateListener? = null

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.AnimationCardView, defStyleAttr, -1)
        isNoAnimation = typedArray.getBoolean(R.styleable.AnimationCardView_noAnimation, false)
        typedArray.recycle()
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        if (isNoAnimation) {
            return
        }
        if (mSpringAnimation == null) {
            mSpringAnimation = SpringAnimation(this, SpringAnimation.SCALE_X)
        }
        mSpringAnimation!!.setSpring(
            SpringForce()
                .setStiffness(SPRING_STIFFNESS)
                .setDampingRatio(SPRING_DAMPING_RATIO)
        )
        mUpdateListener?.let{
            mUpdateListener = OnAnimationUpdateListener { _: DynamicAnimation<*>?, _: Float, _: Float ->
                this.scaleY = this.scaleX
            }
        }
        if (!mSpringAnimation!!.isRunning) {
            mSpringAnimation!!.addUpdateListener(mUpdateListener)
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        if (isNoAnimation) {
            return
        }
        mSpringAnimation?.let {
            it.removeUpdateListener(mUpdateListener)
            it.cancel()
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (isNoAnimation || !isEnabled) {
            return super.onTouchEvent(event)
        }
        when (event.action) {
            MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
                mSpringAnimation!!.spring.setFinalPosition(1f)
                mSpringAnimation!!.start()
                performClick()
            }

            MotionEvent.ACTION_DOWN -> {
                mSpringAnimation!!.spring.setFinalPosition(SCALE_RATIO)
                mSpringAnimation!!.start()
            }

            else -> {}
        }
        return super.onTouchEvent(event)
    }

    override fun performClick(): Boolean {
        Log.d("June", "performClick")
        return super.performClick()
    }

    companion object {
        private const val SPRING_STIFFNESS = 400f
        private const val SPRING_DAMPING_RATIO = 1f
        private const val SCALE_RATIO = 0.95f
    }
}